package com.firefighting.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.firefighting.entity.Proprietor;
import com.firefighting.entity.ProprietorConnector;
import com.firefighting.entity.User;
import com.firefighting.entity.UserBasicInfo;
import com.firefighting.service.EnterpriseService;
import com.firefighting.service.ProprietorConnectorService;
import com.firefighting.service.ProprietorService;
import com.firefighting.service.UserBasicInfoService;

@Controller
@RequestMapping("/Proprietor")
public class ProprietorController
{
	@Autowired
	private ProprietorService proprietorService;
	
	@Autowired
	private ProprietorConnectorService proprietorConnectorService;
	
	@Autowired
	private UserBasicInfoService userBasicInfoService;
	
	@Autowired
	private EnterpriseService enterpriseService;
	/**
	 * @author zhyan
	 * @function 删除业主
	 * @date 2018.6.8
	 * @param id 业主id
	 */
	@RequestMapping("/deleteProprietor")
	@ResponseBody
	private String deleteProprietor(@RequestParam("id") int id)
	{
		System.out.println("id:"+id);
		Map<String,Object> result = new HashMap<String,Object>();
		proprietorConnectorService.deleteByProprietorId(id);//删除业主联系人表中的外键约束
		int number = proprietorService.deleteById(id);//删除业主表中的对应记录
		
		if(number > 0)
		{
			result.put("message", "true");
		}
		else
		{
			result.put("message", "false");
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("flag", true);
		return JSONObject.toJSONString(map);
	}
	
	/**
	 * @author zhyan
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonGenerationException 
	 * @function 查询所有的业主
	 * @date 2018.6.8
	 */
	@RequestMapping("/queryAllProprietor")
	private String queryAllProprietor(HttpServletRequest request, Model model) throws JsonGenerationException, JsonMappingException, IOException
	{
		HttpSession session = request.getSession();
		User currentUser = (User) session.getAttribute("currentUser");
		UserBasicInfo userBasicInfo = userBasicInfoService.getInfoByAccount(currentUser.getUsername());
		int roleId = userBasicInfo.getRoleId();
		ObjectMapper objectMapper = new ObjectMapper();
		
		switch (roleId)
		{
		case 9://消防总队
			String province = userBasicInfo.getProvince();
			List<Integer> enterpriseIdList = enterpriseService.getEnterpriseByProvence(province);
			@SuppressWarnings("rawtypes") List<HashMap> content2 = proprietorService.queryProprietorByEnterterpriseIdList(enterpriseIdList);
			model.addAttribute("content", objectMapper.writeValueAsString(content2));
			break;
		case 10://消防支队
			String province1 = userBasicInfo.getProvince();
			String city = userBasicInfo.getCity();
			List<Integer> enterpriseIdList1 = enterpriseService.queryEnterpriseIdListByProvinceAndCity(province1, city);
			@SuppressWarnings("rawtypes") List<HashMap> content3 = proprietorService.queryProprietorByEnterterpriseIdList(enterpriseIdList1);
			model.addAttribute("content", objectMapper.writeValueAsString(content3));
			break;
		case 2://项目负责人
			String logId = userBasicInfo.getLoginId();
			@SuppressWarnings("rawtypes") List<HashMap> content1 = proprietorService.queryProprietorByLoginId(logId);
			model.addAttribute("content", objectMapper.writeValueAsString(content1));
			break;
		case 1://企业管理员
			@SuppressWarnings("rawtypes")
			List<HashMap> content = proprietorService.queryProprietorByEnter(userBasicInfo.getEnterpriseId());
			model.addAttribute("content", objectMapper.writeValueAsString(content));
			break;
		}
		
		return "proprietor/proprietor";
	}
	
	/**
	 * @author 韩佳迪
	 * @function 添加业主及其对应的两个联系人
	 * @date 2018.6.10
	 * @return
	 */
	@RequestMapping("/toProprietor")
	public String toProprietor(Model model){
		return "proprietor/addProprietor";
		
	}
	
	/**
	 * @author zhyan
	 * @param 添加业主
	 * @date 2018.6.10
	 */
	@RequestMapping("/addProprietor")
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	public String addProprietor(HttpServletRequest request,Model model)
	{	
		
		HttpSession session = request.getSession();
		User currentUser = (User) session.getAttribute("currentUser");
		UserBasicInfo userBasicInfo = userBasicInfoService.getInfoByAccount(currentUser.getUsername());
		
		//添加业主
		String name = request.getParameter("name");
		String address = request.getParameter("address");
		
		Proprietor proprietor = new Proprietor();
		proprietor.setAddress(address);
		proprietor.setName(name);
		proprietor.setStatus("true");
		proprietor.setEnterpriseId(userBasicInfo.getEnterpriseId());
/*		if(proprietorService.queryWithNameAndAddress(name, address) != null)
		{
			model.addAttribute("message", "1");//已经存在该用户名
			return "proprietor/addProprietor";
		}*/
		
		proprietorService.addProprietor(proprietor);//将数据插入数据库
		int id = proprietorService.queryIdWithNameAndAddress(name, address);
		
		//从前端获取业主联系人信息
		String contactor_name = request.getParameter("name1");
		String contactor_sex = request.getParameter("gender1");
		String contactor_email = request.getParameter("email1");
		String contactor_qq = request.getParameter("qq1");
		String contactor_wechat = request.getParameter("wechat1");
		String contactor_phone_number = request.getParameter("phone_number1");
		String contactor_department = request.getParameter("department1");
		String contactor_IDCard = request.getParameter("IDCard1");
		String contactor_technical_level = request.getParameter("technical_level1");
		String contactor_position = request.getParameter("position1");
		
		//添加业主联系人信息
		ProprietorConnector connector = new ProprietorConnector();
		connector.setIdentify("业主联系人");
		connector.setProprietorId(id);
		connector.setName(contactor_name);
		connector.setMobilePhone(contactor_phone_number);
		connector.setDepartment(contactor_department);
		connector.setTechnicalLevel(contactor_technical_level);
		connector.setPosition(contactor_position);
		connector.setIdentificationCard(contactor_IDCard);
		connector.setSex(contactor_sex);
		connector.setEmail(contactor_email);
		connector.setQq(contactor_qq);
		connector.setWechat(contactor_wechat);
		proprietorConnectorService.insertProprietorConnector(connector);
		
		//从前端获取现场联系人信息
		String place_name = request.getParameter("name2");
		String place_sex = request.getParameter("gender2");
		String place_email = request.getParameter("email2");
		String place_qq = request.getParameter("qq2");
		String place_wechat = request.getParameter("wechat2");
		String place_phone_number = request.getParameter("phone_number2");
		String place_department = request.getParameter("department2");
		String place_IDCard = request.getParameter("IDCard2");
		String place_technical_level = request.getParameter("technical_level2");
		String place_position = request.getParameter("position2");
		
		//添加现场联系人
		ProprietorConnector place = new ProprietorConnector();
		place.setProprietorId(id);
		place.setSex(place_sex);
		place.setEmail(place_email);
		place.setQq(place_qq);
		place.setWechat(place_wechat);
		place.setIdentify("现场联系人");
		place.setName(place_name);
		place.setDepartment(place_department);
		place.setTechnicalLevel(place_technical_level);
		place.setPosition(place_position);
		place.setIdentificationCard(place_IDCard);
		place.setMobilePhone(place_phone_number);
		proprietorConnectorService.insertProprietorConnector(place);
		
		return "redirect:/Proprietor/queryAllProprietor.htm";
	}
	
	/**
	 * @author 韩佳迪
	 * @functin 编辑业主联系人、现场联系人、业主信息
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/editProprietor")
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	public String editProprietor(HttpServletRequest request,Model model)
	{
		System.out.println("id------"+request.getParameter("id"));
		int id = Integer.parseInt(request.getParameter("id"));
		
		//从前端获取业主信息
		String name = request.getParameter("name");
		String address = request.getParameter("address");
		
		//从前端获取业主联系人信息
		String contactor_name = request.getParameter("name1");
		String contactor_sex = request.getParameter("gender1");
		String contactor_email = request.getParameter("email1");
		String contactor_qq = request.getParameter("qq1");
		String contactor_wechat = request.getParameter("wechat1");
		String contactor_phone_number1 = request.getParameter("phone_number1");
		String contactor_department = request.getParameter("department1");
		String contactor_IDCard1 = request.getParameter("IDCard1");
		String contactor_technical_level = request.getParameter("technical_level1");
		String contactor_position = request.getParameter("position1");
		
		//从前端获取现场联系人信息
		String place_name = request.getParameter("name2");
		String place_sex = request.getParameter("gender2");
		String place_email = request.getParameter("email2");
		String place_qq = request.getParameter("qq2");
		String place_wechat = request.getParameter("wechat2");
		String place_phone_number = request.getParameter("phone_number2");
		String place_department = request.getParameter("department2");
		String place_IDCard = request.getParameter("IDCard2");
		String place_technical_level = request.getParameter("technical_level2");
		String place_position = request.getParameter("position2");
		
		
		//更新业主信息
		Proprietor proprietor = proprietorService.queryProprietor(id);
		proprietor.setName(name);
		proprietor.setAddress(address);
		proprietorService.updateProprietor(proprietor);
		
		int number = proprietorService.updateProprietor(proprietor);
		if(number > 0)
		{
			model.addAttribute("message", "true");//业主更新成功
		}
		else
		{
			model.addAttribute("message", "false");//业主更新失败
		}
		
		//更新业主联系人信息
		ProprietorConnector connector = proprietorConnectorService.queryByProprietorIDAndIdentify(id, "业主联系人");
		connector.setProprietorId(id);
		connector.setName(contactor_name);
		connector.setMobilePhone(contactor_phone_number1);
		connector.setDepartment(contactor_department);
		connector.setIdentificationCard(contactor_IDCard1);
		connector.setTechnicalLevel(contactor_technical_level);
		connector.setPosition(contactor_position);
		connector.setQq(contactor_qq);
		connector.setWechat(contactor_wechat);
		connector.setSex(contactor_sex);
		connector.setEmail(contactor_email);
		
		int number1 = proprietorConnectorService.updateProprietorConnector(connector);
		if(number1 > 0)
		{
			model.addAttribute("message1", "true");//业主联系人更新成功
		}
		else
		{
			model.addAttribute("message1", "false");//业主联系人更新失败
		}
		
		
		//更新现场联系人信息
		ProprietorConnector place = proprietorConnectorService.queryByProprietorIDAndIdentify(id, "现场联系人");
		place.setProprietorId(id);
		place.setName(place_name);
		place.setMobilePhone(place_phone_number);
		place.setDepartment(place_department);
		place.setIdentificationCard(place_IDCard);
		place.setTechnicalLevel(place_technical_level);
		place.setPosition(place_position);
		place.setQq(place_qq);
		place.setWechat(place_wechat);
		place.setSex(place_sex);
		place.setEmail(place_email);
		
		int number2 = proprietorConnectorService.updateProprietorConnector(place);
		if(number2 > 0)
		{
			model.addAttribute("message2", "true");//现场联系人更新成功
		}
		else
		{
			model.addAttribute("message2", "false");//现场联系人更新失败
		}
		
		return "redirect:/Proprietor/queryAllProprietor.htm";
		
	}
	
	/**
	 * @author zhyan
	 * @functino 查询业主，同时展示对应的业主联系人和现场联系人信息
	 * @date 2018.6.10
	 */
	@RequestMapping("/queryProprietor")
	public String queryproprietor(HttpServletRequest request, Model model)
	{
		int id = Integer.parseInt(request.getParameter("id"));
		//System.out.println("id= " + id);
		Proprietor proprietor = proprietorService.queryProprietor(id);
		model.addAttribute("content", proprietor);
		
		ProprietorConnector connector = proprietorConnectorService.queryByProprietorIDAndIdentify(id, "业主联系人");
		model.addAttribute("content1", connector);
		
		ProprietorConnector place = proprietorConnectorService.queryByProprietorIDAndIdentify(id, "现场联系人");
		model.addAttribute("content2", place);
		
		return "proprietor/editProprietor";
	}
}